import { request, checkPhone, checkEmptyStr, WXshowToast } from '../../utils/index.js';
import { Api } from '../../config/api.js';

import regeneratorRuntime from '../../lib/runtime/runtime'; //es7
Page({

 
  data: {
      phone:"",
      password:"",
      code: "",
      disabled:false,
      auth: "获取验证码",
  },

countdown: 60,

  onLoad: function (options) {

  },


handleInput(e) { //公共
        let value = this.validateNumber(e.detail.value)
        this.setData({
             [e.currentTarget.dataset.name]: value
        });
        console.log(this.data)
    },
    validateNumber(val) {
        return val.replace(/\D/g, '')
    },

    //密码
    handleInputpass(e) {
        let value = e.detail.value
        const { password} = this.data;
        this.setData({
           password: value
        });
    },


//发送验证码
    async getMsgCode() {
        const { phone} = this.data
        const isValid = checkPhone(phone);

        if (!isValid) {
            WXshowToast('请输入正确手机号');
            return;
        }

       // 倒计时
       this.setData({
           disabled: true
       })

       var tiem = setInterval(() => {
           if (this.countdown == 0) {
               this.setData({
                   auth: "获取验证码",
                   disabled: false
               })
               clearInterval(tiem)
               this.countdown = 60
           } else {
                  this.countdown--
               this.setData({
                   disabled: true,
                   auth: `${this.countdown}后重新获取`,
               })
           }

       }, 1000)


    let data =  await   request({
            url: Api.getMsgCode,
                data: {
                    phone
                },
                method: 'POST'
        })
        
    },

    // 确定
    async start(){
         const { phone,code,password} = this.data
        const isValid = checkPhone(phone);
        if (!isValid) {
            WXshowToast('请输入正确手机号');
            return;
        }else if(!code){
              WXshowToast('请输入验证码');
              return;
        }else if(!password){
             WXshowToast('请输入密码');
             return;
        }else{
            const data = await request ({
                url: Api.updataPass,
                data:{
                    phone,
                    code,
                    password
                }
            })
            console.log(data)
            if(data.data.code==2001){
                wx.navigateTo({
                    url: '/pages/login/login',
                });
            }else{
                WXshowToast(data.data.msg);
            }
        }
    }

})